// https://leetcode.cn/problems/perfect-squares/

class Solution {
public:
    int numSquares(int n) {
        vector<int> rec(n + 1, n + 1);
        rec[0] = 0;
        for (int i = 1; i <= n; ++i) {
            for (int j = 0; j <= sqrt(i); ++j) {
                rec[i] = min(rec[i], rec[i - j * j] + 1);
            }
        }
        return rec.back();
    }
};